﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DoAnBLL;

namespace DoAn.Controllers
{ 
    public class TTNgDungController : Controller
    {
        private BLUSIEntities db = new BLUSIEntities();

        //
        // GET: /TTNgDung/

        public ViewResult Index()
        {
            var ttnguoidung = db.ttnguoidung.Include("taikhoan");
            return View(ttnguoidung.ToList());
        }

        public ViewResult TTTK()
        {
            var ttnguoidung = db.ttnguoidung.Include("taikhoan");
            return View(ttnguoidung.ToList());
        }
        //
        // GET: /TTNgDung/Details/5

        public ViewResult Details(int id)
        {
            ttnguoidung ttnguoidung = db.ttnguoidung.Single(t => t.ID == id);
            return View(ttnguoidung);
        }

        public ActionResult TimKiem(string searhstring)
        {
            var ngdung = from s in db.ttnguoidung  select s;

            if (!String.IsNullOrEmpty(searhstring))
            {
                ngdung = ngdung.Where(s => s.HOTEN.ToLower().Contains(searhstring.ToLower()));
            }
            return View(ngdung.ToList ());
        }
        //
        // GET: /TTNgDung/Create

        public ActionResult Create()
        {
            ViewBag.USERNAME = new SelectList(db.taikhoan, "USERNAME", "PASS");
            return View();
        } 

        //
        // POST: /TTNgDung/Create

        [HttpPost]
        public ActionResult Create(ttnguoidung ttnguoidung)
        {
            if (ModelState.IsValid)
            {
                db.ttnguoidung.AddObject(ttnguoidung);
                db.SaveChanges();
                return RedirectToAction("Index");  
            }

            ViewBag.USERNAME = new SelectList(db.taikhoan, "USERNAME", "PASS", ttnguoidung.USERNAME);
            return View(ttnguoidung);
        }
        
        //
        // GET: /TTNgDung/Edit/5
 
        public ActionResult Edit(int id)
        {
            ttnguoidung ttnguoidung = db.ttnguoidung.Single(t => t.ID == id);
            ViewBag.USERNAME = new SelectList(db.taikhoan, "USERNAME", "USERNAME", ttnguoidung.USERNAME);
            return View(ttnguoidung);
        }

        //
        // POST: /TTNgDung/Edit/5

        [HttpPost]
        public ActionResult Edit(ttnguoidung ttnguoidung)
        {
            if (ModelState.IsValid)
            {
                db.ttnguoidung.Attach(ttnguoidung);
                db.ObjectStateManager.ChangeObjectState(ttnguoidung, EntityState.Modified);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.USERNAME = new SelectList(db.taikhoan, "USERNAME", "USERNAME", ttnguoidung.USERNAME);
            return View(ttnguoidung);
        }

        //
        // GET: /TTNgDung/Delete/5
 
        public ActionResult Delete(int id)
        {
            ttnguoidung ttnguoidung = db.ttnguoidung.Single(t => t.ID == id);
            return View(ttnguoidung);
        }

        //
        // POST: /TTNgDung/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {            
            ttnguoidung ttnguoidung = db.ttnguoidung.Single(t => t.ID == id);
            db.ttnguoidung.DeleteObject(ttnguoidung);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}